<?php
session_start();
if (isset($_SESSION['openid']) && isset($_POST['name'])){
    include '../c/config.php';
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    $name=addslashes(trim($_POST['name']));
    
    $queryName=$mysqli->query("select id from ".$tableVer."_team where name='".$name."' limit 0, 1");
    if($queryName->num_rows>0){
        echo json_encode(array('s'=>'n','e'=>'name'));
        exit;
    }
    
    $queryG=$mysqli->query("select id from ".$tableVer."_group where openid='".$_SESSION['openid']."' limit 0, 1");
    if($queryG->num_rows>0){
        echo json_encode(array('s'=>'y'));
        exit;
    }
    
    $mysqli->query("insert into ".$tableVer."_team(openid,name,createtime) values('".$_SESSION['openid']."','".$name."','".date("Y-m-d H:i:s")."') ");
    
    $queryT=$mysqli->query("select id from ".$tableVer."_team where openid='".$_SESSION['openid']."' and name='".$name."' limit 0, 1");
    if($queryT->num_rows>0){
        $getTeamId=$queryT->fetch_assoc();
        
        $queryU=$mysqli->query("select headimg,nickname from ".$tableVer."_users where openid='".$_SESSION['openid']."' limit 0, 1");
        $getU=$queryU->fetch_assoc();
        $mysqli->query("insert into ".$tableVer."_group(openid,tid,headimg,nickname,createtime) values('".$_SESSION['openid']."',".$getTeamId['id'].",'".$getU['headimg']."','".$getU['nickname']."','".date("Y-m-d H:i:s")."') ");
    }
    echo json_encode(array('s'=>'y'));
}
?>
